using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[DefaultExecutionOrder(100)]
public class LineManager : MonoBehaviour
{
    public LineRenderer line;
    public string beCutLineName;
    public int cutIndex; // 被切的index
    public bool cut;
    private bool destroyed;
    private bool isReset;

    private void Start()
    {
        initLine();
    }

    private void initLine()
    {
        line = gameObject.AddComponent<LineRenderer>();
        line.material = RopeManager.instance.lineMaterial;
        line.startWidth = 0.25f;
        line.endWidth = 0.25f;
        line.textureMode = LineTextureMode.Tile;
    }

    private void Update()
    {
        if (cut)
        {
            if (!destroyed)
            {
                DestroyRope();
                destroyed = false;
                cut = false;
            }
        }
        if (line != null)
        {
            resetPosition();
        }
        if (GameManager.instance.win)
        {
            Invoke("destroyLine", 1.5f);
            return;
        }
    }

    private void DestroyRope()
    {
        Candy candyCs = GameManager.instance.candy.GetComponent<Candy>();
        if (candyCs.fixs.Contains(transform))
        {
            candyCs.fixs.Remove(transform);
        }
        // 禁用掉以前的每一个碰撞体
        for (int i = 0; i < transform.childCount; i++)
        {
            // transform.GetChild(i).gameObject.SetActive(false);
            transform.GetChild(i).GetComponent<CircleCollider2D>().enabled = false;
        }
        // 生成新的 line1
        GameObject lineGObj1 = new GameObject("line1");
        CutRope CutRope1 = lineGObj1.AddComponent<CutRope>();
        CutRope1.tsList.Add(transform);
        for (int i = 0; i < cutIndex; i++)
        {
            CutRope1.tsList.Add(transform.GetChild(i));
        }

        // 生成新的 line2
        // GameObject lineGObj2 = new GameObject("line2");
        // CutRope CutRope2 = lineGObj2.AddComponent<CutRope>();
        // CutRope2.tsList.Add(RopeManager.instance.candy.transform);


        // 销毁原来的
        Destroy(line); // LineRenderer
        // Destroy(transform.GetChild(cutIndex).gameObject); // 被切割（碰撞）的那个gameObject
        // 通知RopeManager哪一个节点已经断开，即让gameObject移除HingeJoint2D组件
        if (transform.name == beCutLineName)
        {
            // transform.GetChild(cutIndex).gameObject.GetComponent<HingeJoint2D>().enabled = false;
            RopeManager.instance.cutIndex = cutIndex;
            RopeManager.instance.beCutLineName = beCutLineName;
            int lastIndex = transform.childCount - 1;
            for (int destroyIndex = lastIndex; destroyIndex > cutIndex; destroyIndex--)
            {
                GameObject nodeGo = transform.GetChild(destroyIndex).gameObject;
                Debug.Log("nodeGo");
                Debug.Log(nodeGo);
                if (nodeGo != null)
                {
                    Destroy(nodeGo);
                }
            }
            RopeManager.instance.SendMessage("removeHingeJoint2D");
        }
    }

    private void resetPosition()
    {
        line.SetPosition(0, transform.position);

        if (GameManager.instance.win)
        {
            line.positionCount = transform.childCount + 1;
            int i = 0;
            for (i = 0; i < transform.childCount; i++)
            {
                line.SetPosition(i + 1, transform.GetChild(i).position);
            }
            return;
        }
        else
        {
            line.positionCount = transform.childCount + 2;
            int i = 0;
            for (i = 0; i < transform.childCount; i++)
            {
                line.SetPosition(i + 1, transform.GetChild(i).position);
            }
            line.SetPosition(i + 1, RopeManager.instance.candy.transform.position);
        }
    }

    public void destroyLine()
    {
        LineRenderer line = gameObject.GetComponent<LineRenderer>();
        if (line != null)
        {
            Destroy(line);
        }
    }
}
